import java.sql.*;

public class Main {
    public static void main(String[] args) {
        // 数据库URL
        String url = "jdbc:mysql://localhost:3306/jdbc";
        // 用户名
        String user = "root";
        // 密码
        String password = "root";

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            // 1. 加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2. 建立连接
            conn = DriverManager.getConnection(url, user, password);

            // 3. 创建Statement对象
            String sql = "SELECT * FROM user3 WHERE id = ?";
            pstmt = conn.prepareStatement(sql);

            // 4. 设置参数值
            pstmt.setInt(1, 1);

            // 5. 执行查询
            rs = pstmt.executeQuery();

            // 6. 处理结果集
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String pass = rs.getString("pass");
                System.out.println("ID: " + id + ", Name: " + name + ", Email: " + pass);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 7. 关闭资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}